package com.liangsui.mss.srevice.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.Registration;
import com.liangsui.mss.dto.RegistrationDto;
import com.liangsui.mss.mapper.RegistrationMapper;
import com.liangsui.mss.service.RegistrationService;
import com.liangsui.mss.vo.DataGridView;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author zhang addRegistration
* @description 针对表【his_registration】的数据库操作Service实现
* @createDate 2022-12-06 22:36:39
*/
@DubboService(methods = {@Method(name="addRegistration",retries = 0)}, timeout = 10000)
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements RegistrationService{

    @Autowired
    private RegistrationMapper registrationMapper;

    @Override
    public DataGridView queryRegistrationForPage(RegistrationDto registrationDto) {
        Page<Registration> page = new Page<>(registrationDto.getPageNum(), registrationDto.getPageSize());
        QueryWrapper<Registration> qw = new QueryWrapper<>();
        qw.eq(registrationDto.getDeptId() != null,
                Registration.COL_DEPT_ID, registrationDto.getDeptId());
        qw.like(StringUtils.isNotBlank(registrationDto.getPatientName()),
                Registration.COL_PATIENT_NAME, registrationDto.getPatientName());
        qw.eq(StringUtils.isNotBlank(registrationDto.getSchedulingType()),
                Registration.COL_SCHEDULING_TYPE, registrationDto.getSchedulingType());
        qw.eq(StringUtils.isNotBlank(registrationDto.getSubsectionType()),
                Registration.COL_SUBSECTION_TYPE, registrationDto.getSubsectionType());
        qw.eq(StringUtils.isNotBlank(registrationDto.getRegStatus()),
                Registration.COL_REG_STATUS, registrationDto.getRegStatus());
        qw.eq(StringUtils.isNotBlank(registrationDto.getVisitDate()),
                Registration.COL_VISIT_DATE, registrationDto.getVisitDate());
        qw.orderByDesc(Registration.COL_CREATE_TIME);
        this.registrationMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

	@Override
	public Registration queryRegistrationByRegId(String registrationId) {
		return this.registrationMapper.selectById(registrationId);
	}

	@Override
	public int updateRegistrationById(Registration registration) {
		return this.registrationMapper.updateById(registration);
	}

    /**
     *
     * @param deptId
     * @param subsectionType 时间段
     * @param scheudlingType 门诊 急诊
     * @param regStatus 1-待就诊
     * @param userId 医生id
     * @return
     */
    @Override
    public List<Registration> queryRegistration(Long deptId, String subsectionType, String scheudlingType, String regStatus, Long userId) {
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq(Registration.COL_DEPT_ID, deptId);
        wrapper.eq(StringUtils.isNotBlank(subsectionType),Registration.COL_SUBSECTION_TYPE, subsectionType);
        wrapper.eq(Registration.COL_SCHEDULING_TYPE, scheudlingType);
        wrapper.eq(Registration.COL_REG_STATUS, regStatus);
        // 解决查询挂号日期Bug
        wrapper.eq(Registration.COL_VISIT_DATE, DateUtil.format(DateUtil.date(),"yyyy-MM-dd"));
        wrapper.eq(null != userId, Registration.COL_USER_ID, userId);
        wrapper.orderByAsc(Registration.COL_REG_NUMBER);
        return this.registrationMapper.selectList(wrapper);
    }

//    @Override
//    public DataGridView queryRegistrationForPage(RegistrationDto registrationDto) {
//        Page<Registration> page=new Page<Registration>(registrationDto.getPageNum(),registrationDto.getPageSize());
//        QueryWrapper<Registration> qw=new QueryWrapper<>();
//        qw.eq(registrationDto.getDeptId()!=null,Registration.COL_DEPT_ID,registrationDto.getDeptId());
//        qw.like(StringUtils.isNotBlank(registrationDto.getPatientName()),Registration.COL_PATIENT_NAME,registrationDto.getPatientName());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getSchedulingType()),Registration.COL_SCHEDULING_TYPE,registrationDto.getSchedulingType());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getSubsectionType()),Registration.COL_SUBSECTION_TYPE,registrationDto.getSubsectionType());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getRegStatus()),Registration.COL_REG_STATUS,registrationDto.getRegStatus());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getVisitDate()),Registration.COL_VISIT_DATE,registrationDto.getVisitDate());
//        qw.orderByDesc(Registration.COL_CREATE_TIME);
//        this.registrationMapper.selectPage(page,qw);
//        return new DataGridView(page.getTotal(),page.getRecords());
//    }

    /**
     * 根据患者id查询患者信息
     * @param patientId
     * @return
     */
    @Override
    public List<Registration> getRegistrationsByPatientId(String patientId) {
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq(Registration.COL_PATIENT_ID, patientId);
        List<Registration> registrations = registrationMapper.selectList(wrapper);
        return registrations;
    }

//    /**
//     * 修改患者信息
//     * @param registration
//     * @return
//     */
//    @Override
//    public int updateRegistrationById(Registration registration) {
//        return this.registrationMapper.updateById(registration);
//    }

    @Override
    public void addRegistration(RegistrationDto registrationDto) {
        Registration registration = new Registration();
        BeanUtil.copyProperties(registrationDto, registration);
        registration.setRegStatus(Constants.REG_STATUS_0);
        registration.setCreateBy(registrationDto.getSimpleUser().getUserName());
        registration.setCreateTime(DateUtil.date());
        this.registrationMapper.insert(registration);
    }
}




